﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Data;
using System.Collections;
using System.Xml;
using Aspose.Words;
using Maticsoft.DBUtility;

namespace SkWeb.FASJ
{
    public partial class Comment : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!IsPostBack)
                {
                    btnFirst.Text = "最首页";
                    btnPrev.Text = "前一页";
                    btnNext.Text = "下一页";
                    btnLast.Text = "最后页";
                    ViewState["Filter"] = "";


                    string strSql = "select table_name,comments from  user_tab_comments where table_type='TABLE' order by table_name";
                    DataTable dt = OracleHelper.Query(strSql).Tables[0];
                    this.ddlTname.DataSource = dt.DefaultView;
                    //  this.ddlTname.DataValueField = dt.Columns[1].ColumnName;
                    this.ddlTname.DataTextField = dt.Columns[0].ColumnName;
                    this.ddlTname.DataBind();

                    int i = 0;
                    foreach (DataRow dr in dt.Rows)
                    {
                        if (!string.IsNullOrEmpty(dr[1].ToString()))
                            this.ddlTname.Items[i++].Value = dr[1].ToString();
                        else
                            this.ddlTname.Items[i++].Value = dr[0].ToString();

                    }

                    this.txtComment.Text = this.ddlTname.SelectedValue;


                    GridViewTable_Load();
                    
                }

               
               // this.ddlTname.b
            }
        }

        private void GridViewTable_Load()
        {
            //DataTable dt = new DataTable();
            //dt = OracleHelper.Query("select jh,xh,qyrq from as03").Tables[0];

            //GridView1.DataSource = dt.DefaultView;
            //GridView1.DataBind();
            this.txtComment.Text = this.ddlTname.SelectedValue;
            string strName = this.ddlTname.SelectedItem.Text;
            string strSql = " select column_name,comments from  user_col_comments where table_name='" + strName + "' ";
            DataTable dt = OracleHelper.Query(strSql).Tables[0];
            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();
            ViewState["rowCount"] = dt.Rows.Count;
           
            lblCurrentIndex.Text = "第<font color='red'>" + (GridView1.PageIndex + 1).ToString() + " </font>页";
            lblPageCount.Text = "总共 <font color='red'>" + GridView1.PageCount.ToString() + " </font>页";
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
         

        }

        //save event
        protected void btnSave_Click(object sender, EventArgs e)
        {
          
             ArrayList listSqls = new ArrayList();
            string strTableName = this.ddlTname.SelectedItem.Text;
            foreach(GridViewRow gr in GridView1.Rows)
            {
                if (gr.RowType == DataControlRowType.DataRow)
                {
                    TextBox txtBox = gr.FindControl("txtComment") as TextBox;
                    string strComment = txtBox.Text;
                    string strSql = " comment on column  " + strTableName + "." + gr.Cells[0].Text + " is '" + strComment + "'";
                    listSqls.Add(strSql);
                }
            }
            OracleHelper.ExecuteSqlTran(listSqls);
            this.labMessage.Text = "保存成功！";
        }

        protected void PagerButtonClick(object sender, EventArgs e)
        {
            string arg = ((LinkButton)sender).CommandArgument.ToString();
            switch (arg)
            {
                case "next":
                    GridView1.PageIndex = GridView1.PageIndex + 1;
                    break;
                case "prev":
                    if (GridView1.PageIndex > 0)
                    {
                        GridView1.PageIndex = GridView1.PageIndex - 1;
                    }
                    break;
                case "last":
                    GridView1.PageIndex = GridView1.PageCount - 1;
                    break;
                case "0":
                    GridView1.PageIndex = 0;
                    break;
            }
            GridViewTable_Load();
        }


       
        /// <summary>
        /// 更改表备注
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMdyCom_Click(object sender, EventArgs e)//
        {
            try
            {
                string tableName = this.ddlTname.SelectedItem.Text.Trim();
                string strComm = this.txtComment.Text.Trim();
                string strSql = " comment on table  " + tableName + "  is '" + strComm + "' ";
              //  string strSql = "update user_tab_comments set comments='" + strComm + "' where table_name='" + tableName + "' ";
                OracleHelper.ExecuteSql(strSql);
                this.labMessage.Text = "修改成功";
            }
            catch (Exception ex)
            {
                this.labMessage.Text = ex.Message;
            }
        }

        protected void ddlTname_SelectedIndexChanged(object sender, EventArgs e)
        {

            GridViewTable_Load();
        }

    }

  
}